package com.strat.searcher.lucene.indexing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.strat.searcher.exceptions.IndexingException;
import com.strat.searcher.lucene.files.FileUtil;

public class IndexWriterWrapper implements IndexWriterable {

    private IndexWriter writer;
    private final String CONSTRUCTOR_ERROR = "CONSTRUCTOR_ERROR";
    private final String CLOSING_ERROR = "CLOSING_ERROR";
    private final String ACCESS_ERROR = "ACCESS_ERROR";
    private final String DOCUMENT_ACCESS_ERROR = "DOCUMENT_ACCESS_ERROR";
    private final String INDEX_ACCESS_ERROR = "INDEX_ACCESS_ERROR";

    private static Logger logger = LoggerFactory
            .getLogger(IndexWriterWrapper.class);

    public IndexWriterWrapper(File indexDir) {
        Directory dir = FileUtil.getDirectory(indexDir);
        try {
            writer = new IndexWriter(dir, new StandardAnalyzer(
                    Version.LUCENE_30), true,
                    IndexWriter.MaxFieldLength.UNLIMITED);
        } catch (IOException e) {
            logger.error(CONSTRUCTOR_ERROR, e);
            throw new IndexingException(CONSTRUCTOR_ERROR, e);
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.strat.searcher.lucene.indexing.IndexWriterable#addDocument(java.io
     * .File)
     */
    @Override
    public void addDocument(File f) {
        try {
            logger.info("Trying to index " + f.getCanonicalPath());
            Document doc;
            doc = getDocument(f);
            writer.addDocument(doc);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new IndexingException(DOCUMENT_ACCESS_ERROR, e);
        } catch (CorruptIndexException e) {
            logger.error("fail", e);
            throw new IndexingException(INDEX_ACCESS_ERROR, e);
        } catch (IOException e) {
            logger.error("fail", e);
            throw new IndexingException(DOCUMENT_ACCESS_ERROR, e);
        }

    }

    protected Document getDocument(File f) throws FileNotFoundException {
        Document doc = new Document();
        doc.add(new Field("contents", new FileReader(f)));
        return doc;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.strat.searcher.lucene.indexing.IndexWriterable#numDocs()
     */
    @Override
    public int numDocs() {
        try {
            return writer.numDocs();
        } catch (IOException e) {
            logger.error("fail", e);
            throw new IndexingException(ACCESS_ERROR, e);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.strat.searcher.lucene.indexing.IndexWriterable#close()
     */
    @Override
    public void close() {
        try {
            writer.close();
        } catch (CorruptIndexException e) {
            logger.error("fail", e);
            throw new IndexingException(CLOSING_ERROR, e);
        } catch (IOException e) {
            logger.error("fail", e);
            throw new IndexingException(CLOSING_ERROR, e);
        }
    }

    public static void main(String[] args) {
        logger.info("dfg");
    }
}
